package com.hzeport.bgs22emduty.enums;

import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

public enum DTypeEnum implements BaseEnum {

    TYPE_MORNING("1", "上午", 3),
    TYPE_AFTERNOON("2", "下午", 4),
    TYPE_EVENING("3", "夜间", 5),
    TYPE_ALLDAY("4", "全天",1),
    TYPE_OTHER("5", "其他", 6),
    TYPE_DAY_TIME("6", "白天", 2);

    private final String code;
    private final String description;
    @Getter
    private final int sort;

    DTypeEnum(String code, String description, int sort) {
        this.code = code;
        this.description = description;
        this.sort = sort;
    }

    @Override
    public String toString() {
        return this.getValue();
    }

    @Override
    public String getValue() {
        return code;
    }

    public String getDesc() {
        return description;
    }

    @Override
    public BaseEnum getEnum(String value) {
        for (BaseEnum v : values())
            if (v.getValue().equalsIgnoreCase(value)) return v;
        throw new IllegalArgumentException();
    }

    public String getValueByCode(String code) {
        for (DTypeEnum v : values())
            if (v.getValue().equalsIgnoreCase(code)) return v.getDescription();
        return code;
    }

    public String getCodeByValue(String value) {
        for (DTypeEnum v : values())
            if (v.getDescription().equalsIgnoreCase(value)) return v.getValue();
        return value;
    }

    public Map<String, Object> toMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", getValue());
        map.put("description ", getDescription());
        return map;
    }

    public static String getDescByCode(String code) {
        if (StringUtils.isNotEmpty(code)) {
            for (DTypeEnum myEnum : DTypeEnum.values()) {
                if (StringUtils.equals(myEnum.getValue(), code)) {
                    return myEnum.getDesc();
                }
            }
        }
        return "";
    }

    public static int getSortByCode(String code) {
        if (StringUtils.isNotEmpty(code)) {
            for (DTypeEnum myEnum : DTypeEnum.values()) {
                if (StringUtils.equals(myEnum.getValue(), code)) {
                    return myEnum.getSort();
                }
            }
        }
        return Integer.MAX_VALUE;
    }

    @JsonValue
    public String getDescription() {
        return description;
    }
}
